/**
 * File TestUser.java 
 */
package cstest.entity;

import static org.junit.Assert.*;

import java.net.InetAddress;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cs.entity.User;

/**
 * Package  cstest.entity
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 *
 */
public class testUser {
	User u1,  	// for constructor without parameter
	u2 , 		// for constructor with parameter
	u3;			// for other tests
	String usernameTest, newUsername; 
	InetAddress ipTest, ipNew ; 
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		usernameTest = "Username"; 
		newUsername = "newUsername";
		ipTest = InetAddress.getByName("127.0.0.1");
		ipNew =  InetAddress.getByName("127.0.0.2");

		u3 = new User(usernameTest,ipTest) ; 

	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		u1 = null ; 
		u2 = null ; 
		u3 = null ; 
		usernameTest = null ;
		newUsername = null ;
		ipTest = null ; 
		ipTest = null ; 
	}



	/**
	 * Test method for {@link cs.entity.User#User()}.
	 */
	@Test
	public void testUser() {
		assertNull(u1); 
		u1 = new User(); 
		assertNotNull(u1); 
		assertNull(u1.getIpAddress());
		assertNull(u1.getUsername()); 
	}

	/**
	 * Test method for {@link cs.entity.User#User(java.lang.String, java.net.InetAddress)}.
	 */
	@Test
	public void testUserStringInetAddress() {
		assertNull(u2); 
		u2 = new User(usernameTest,ipTest) ; 
		assertNotNull(u2); 
		assertTrue(u2.getIpAddress().equals(ipTest));
		assertTrue(u2.getUsername().equals(usernameTest));

	}

	/**
	 * Test method for {@link cs.entity.User#getUsername()}.
	 */
	@Test
	public void testGetUsername() {
		String test = u3.getUsername(); 
		assertTrue(test.equals(usernameTest)); 
	}

	/**
	 * Test method for {@link cs.entity.User#setUsername(java.lang.String)}.
	 */
	@Test
	public void testSetUsername() {
		assertFalse(u3.getUsername().equals(newUsername)); 
		u3.setUsername(newUsername); 
		assertTrue(u3.getUsername().equals(newUsername)); 
	}

	/**
	 * Test method for {@link cs.entity.User#getIpAddress()}.
	 */
	@Test
	public void testGetIpAddress() {
		assertTrue(u3.getIpAddress().equals(ipTest)); 
	}

	/**
	 * Test method for {@link cs.entity.User#setIpAddress(java.net.InetAddress)}.
	 */
	@Test
	public void testSetIpAddress() {
		assertFalse(u3.getIpAddress().equals(ipNew)); 
		u3.setIpAddress(ipNew); 
		assertTrue(u3.getIpAddress().equals(ipNew)); 
	}

	/**
	 * Test method for {@link cs.entity.User#toString()}.
	 */
	@Test
	public void testToString() {
		String u3toString = u3.toString();
		assertTrue(u3toString.equals("Username (/127.0.0.1)")); 
	}

	/**
	 * Test method for {@link cs.entity.User#equals(java.lang.Object)}.
	 */
	@Test
	public void testEqualsObject() {
		User u4 = new User(newUsername,ipTest) ; // same ip but username different
		User u5 = new User(usernameTest,ipTest) ; // same ip, same username
		User u6 = new User(usernameTest,ipNew) ; // same username, ip different
		User u7 = new User(newUsername,ipNew) ; // different username, ip different
		assertTrue(u3.equals(u4));
		assertTrue(u3.equals(u5));
		assertFalse(u3.equals(u6));
		assertFalse(u3.equals(u7)); 
	}

}
